<?php

/**
 * @BelongProject: flamiManage
 * @CreateTime: 2025-10-07 16:00:59
 * @Description: TODO
 */

namespace app\admin\service;

use PhpOffice\PhpSpreadsheet\Calculation\Token\Stack;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

class CountryService
{
    private const DefaultLangId = 266;

    /**
     * 获取所有的国家
     * @param $type
     * @return array|\think\Collection|Db[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAll($type = 'JSON')
    {
        $query = Db::name('country');

        if (strtoupper($type) == 'JSON') {
            return $query->column('*', 'id');
        }else {
            return $query->select();
        }
    }

    /**
     * 获取推荐的国家
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getRecommendLanguage(): array
    {
        $where = [
            'recommend' => 2,
            'type'      => 1
        ];
        $dataList = Db::name('country')->where($where)->order('title asc')->select()->toArray();
        foreach ($dataList as &$val) {
            $val['nameStr'] = $val['name'];
            $val['name'] = $val['name_cn'];
        }

        return $dataList;
    }

    /**
     * 获取系统默认的语言
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getDefaultLangData(): array
    {
        $where = [
            'id' => self::DefaultLangId
        ];
        return Db::name('country')->where($where)->find();
    }

    /**
     * 根据ID~获取语言内容
     * @param $id
     * @return array|mixed|Db|\think\Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getOne($id)
    {
        $where = [
            'id' => $id
        ];
        return Db::name('country')->where($where)->find();
    }



}